package JAVA300.第十一章多线程;

/**
 * 高级主题3
 * 测试代码重排机制
 */

public class thread_19_others_3HappenBefore {

    private static  int a=0;
    private static  boolean flag=false;
    public static void main(String[] args) {

        //线程1-更改数据
        Thread t1 = new Thread(()->{
            a=1;
            flag = true;
        });

        //线程2-读取数据
        Thread t2 = new Thread(()->{
            if (flag){
                a *= 1;
            }
            if (a == 0){//如果按照顺序 不可能进入这个if
                System.out.println("提前读取了"+a);

            }
        });
        t1.start();
        t2.start();

        //合并线程
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
